package com.gxa.modules.service.impl;

import com.gxa.common.utils.UploadController;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.CarMapper;
import com.gxa.modules.mapper.CysMapper;
import com.gxa.modules.pojo.Car;
import com.gxa.modules.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CysMapper cysMapper;

    @Override
    public JsonResult findCar(HashMap map) {
        JsonResult jsonResult = new JsonResult();
        int currentPage = Integer.parseInt(map.get("currentPage").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        currentPage = (currentPage-1)*pageSize;
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        try {
            //查询总数
            Integer count = carMapper.findzs(map);
            //查询车辆
            List<Car> list = carMapper.findCar(map);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setCount(count);
            jsonResult.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }


    /**
     * 添加车辆
     * @param map
     * @return
     */
    @Override
    public R addCar(HashMap map) {
        R r = new R();
        try {
            Date date = new Date();
            SimpleDateFormat s = new SimpleDateFormat("yyyy.MM.dd");
            String dateTime = s.format(date);
            map.put("new_time",dateTime);
            Integer result = carMapper.addCar(map);
            if (result > 0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R findCayById(Integer car_id) {
        R r = new R();
        try {
            Car car = carMapper.findCayById(car_id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(car);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 编辑
     * @param map
     * @return
     */
    @Override
    public R updateCar(HashMap map) {
        R r = new R();
        try {
            Integer result = carMapper.updateCar(map);
            if (result > 0){
                r.setCode(200);
                r.setMsg("成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 添加图片
     * @param file
     * @return
     */
    @Override
    public R addImg(MultipartFile file) {
        R r = new R();
        try {
            UploadController uploadController = new UploadController();
            HashMap upload = (HashMap) uploadController.upload(file);
            r.setCode(200);
            r.setMsg("成功");
            r.setData(upload.get("url").toString());
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    public void addCysRole(HashMap map){
//        try {
        Integer id = cysMapper.findCysByName(map);
        cysMapper.addCysRole(id);
//        }catch (Exception e){
//            e.printStackTrace();
//            System.out.println("添加司机权限失败");
//        }
    }
}
